package com.huawei;

import java.io.*;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.*;


/**
 * Description: HJ26 字符串排序
 *
 * 1、注意循环输入
 * 2、Scanner.next   Scanner.nextLine区别
 * @author weiruibai.vendor
 * Date: 2022/8/19 09:22
 */
public class HJ26 {


    public static void main(String[] args)throws IOException {

    }

    private static void test2() {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String str = scanner.nextLine();
            List<Character> letters = new ArrayList<>();
            for (int i = 0; i < str.length(); i++) {
                if (Character.isLetter(str.charAt(i))) {
                    letters.add(str.charAt(i));
                }
            }
            letters.sort(((o1, o2) -> Character.toUpperCase(o1) - Character.toUpperCase(o2)));
            int k = 0;
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < str.length(); i++) {
                char cha = str.charAt(i);
                if (Character.isLetter(cha)) {
                    sb.append(letters.get(k++));
                } else {
                    sb.append(cha);
                }
            }
            System.out.println(sb.toString());
        }
    }

    private static void test1() {
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String str = in.nextLine();
            String res = sort(str);
            System.out.println(res);
        }
    }

    public static String sort(String str) {
        // 先将英文字母收集起来
        List<Character> letters = new ArrayList<>();
        for (char ch : str.toCharArray()) {
            if (Character.isLetter(ch)) {
                letters.add(ch);
            }
        }
        // 将英文字母先排序好
        letters.sort(new Comparator<Character>() {
            public int compare(Character o1, Character o2) {
                return Character.toLowerCase(o1) - Character.toLowerCase(o2);
            }
        });
        // 若是非英文字母则直接添加
        StringBuilder result = new StringBuilder();
        for (int i = 0, j = 0; i < str.length(); i++) {
            if (Character.isLetter(str.charAt(i))) {
                result.append(letters.get(j++));
            } else {
                result.append(str.charAt(i));
            }
        }
        return result.toString();
    }
}
